/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.AreaPermission;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.AreaPermissionRepository;
import me.zhengjie.modules.system.service.AreaPermissionService;
import me.zhengjie.modules.system.service.dto.AreaPermissionDto;
import me.zhengjie.modules.system.service.dto.AreaPermissionQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.AreaPermissionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AreaPermissionServiceImpl implements AreaPermissionService {

    private final AreaPermissionRepository areaPermissionRepository;
    private final AreaPermissionMapper areaPermissionMapper;

    @Override
    public Map<String,Object> queryAll(AreaPermissionQueryCriteria criteria, Pageable pageable){
        Page<AreaPermission> page = areaPermissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(areaPermissionMapper::toDto));
    }

    @Override
    public List<AreaPermissionDto> queryAll(AreaPermissionQueryCriteria criteria){
        return areaPermissionMapper.toDto(areaPermissionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public AreaPermissionDto findById(Long id) {
        AreaPermission areaPermission = areaPermissionRepository.findById(id).orElseGet(AreaPermission::new);
        ValidationUtil.isNull(areaPermission.getId(),"AreaPermission","id",id);
        return areaPermissionMapper.toDto(areaPermission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AreaPermissionDto create(AreaPermission resources) {
        resources.setCid(0l);
        return areaPermissionMapper.toDto(areaPermissionRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AreaPermission resources) {
        AreaPermission areaPermission = areaPermissionRepository.findById(resources.getId()).orElseGet(AreaPermission::new);
        ValidationUtil.isNull( areaPermission.getId(),"AreaPermission","id",resources.getId());
        areaPermission.copy(resources);
        areaPermissionRepository.save(areaPermission);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            areaPermissionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AreaPermissionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AreaPermissionDto areaPermission : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", areaPermission.getCreateTime());
            map.put("更新时间", areaPermission.getUpdateTime());
            map.put("创建者", areaPermission.getCreateBy());
            map.put("更新者", areaPermission.getUpdateBy());
            map.put("权限方案名称", areaPermission.getName());
            map.put("权限方案描述", areaPermission.getDescription());
            map.put("禁入区区域id", areaPermission.getNezAreaId());
            map.put("禁入区区域姓名", areaPermission.getNezAreaName());
            map.put("禁入区闯入报警 1 开 2.关", areaPermission.getNezBreakIntoAlarm());
            map.put("禁入区报警等级", areaPermission.getNezAlarmLevel());
            map.put("禁入区禁入时间 1 无限制", areaPermission.getNezNoEntryTime());
            map.put("禁入区禁入起始时间", areaPermission.getNezNoEntryTimeStart());
            map.put("禁入区禁入结束时间", areaPermission.getNezNoEntryTimeEnd());
            map.put("禁入区禁入周期 数组格式", areaPermission.getNezCycle());
            map.put("准入区区域id", areaPermission.getAzAreaId());
            map.put("准入区区域姓名", areaPermission.getAzAreaName());
            map.put("离开报警 az_leave_alarm 滞留报警 az_delay_alarm", areaPermission.getAzAlarmType());
            map.put("准入区离开报警 1 开 2.关", areaPermission.getAzLeaveAlarm());
            map.put("准入区报警等级", areaPermission.getAzAlarmLevel());
            map.put("准入区准入时间 1 无限制", areaPermission.getAzEntryTime());
            map.put("准入区准入起始时间", areaPermission.getAzEntryTimeStart());
            map.put("准入区准入结束时间", areaPermission.getAzEntryTimeEnd());
            map.put("准入区准入时间周期  数组格式", areaPermission.getAzEntryTimeCycle());
            map.put("岗位区区域id", areaPermission.getJzAreaId());
            map.put("岗位区区域名称", areaPermission.getJzAreaName());
            map.put("岗位区离开报警 1 开 2关", areaPermission.getJzLeaveAlarm());
            map.put("岗位区离开报警报警等级 1 2 3", areaPermission.getJzLeaveAlarmLevel());
            map.put("岗位串岗报警 1 开 2 关", areaPermission.getJzSkewersAlarm());
            map.put("岗位串岗报警报警等级 1 2 3", areaPermission.getJzSkewersAlarmLevel());
            map.put("岗位区在岗时间 1 无限制", areaPermission.getJzZgTime());
            map.put("岗位区在岗开始时间", areaPermission.getJzZgTimeStart());
            map.put("岗位区在岗结束时间", areaPermission.getJzZgTimeEnd());
            map.put("岗位区在岗周期 数组格式", areaPermission.getJzZgCycle());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}